﻿// MIT License
// 开源地址：https://gitee.com/co1024/AbcMvc
// Copyright (c) 2021-2023 1024
// Abc.Mvc=Furion+EF+SqlSugar+Pear layui admin.

namespace Abc.Web.Mvc.Areas.HtAdmin.Controllers;

/// <summary>
/// </summary>
[NonValidation]
//[AuthorizePolicy]
[AllowAnonymous]
[MaMvc("HtAdmin", controllerDescription: "角色", PCode = "HtAdmin_User")]
public class RoleController : HtAdminBaseController
{
    private readonly ILogger<RoleController> _logger;
    private readonly IHtRoleService _roleService;

    public RoleController(ILogger<RoleController> logger
        , IHtRoleService roleService)
    {
        _logger = logger;
        _roleService = roleService;
    }

    [HttpGet]
    [MaMenu("角色列表")]
    [MaPermission("角色列表", "Index,GetList")]
    public IActionResult Index()
    {
        return View();
    }

    [HttpGet]
    [HttpPost]
    [UnifyResult(typeof(JsonResult))]
    public async Task<JsonResult> GetList(HtRoleQuery searchquery)
    {
        var pagedlist = await _roleService.GetAllByPageToLayui(searchquery);
        return Json(pagedlist);
    }

    /// <summary>
    ///     新增
    /// </summary>
    /// <param name="id">ID</param>
    /// <returns></returns>
    [MaPermission("新增", "Add,Edit")]
    public IActionResult Add(int? id)
    {
        return View("Info", new HtRoleOut { IsEnable = true });
    }

    [MaPermission("编辑", "Info,Edit")]
    public async Task<ActionResult> Info(int? id)
    {
        HtRoleOut entity = null;
        if (id.HasValue) entity = await _roleService.Get(id.Value);
        return View(entity ?? new HtRoleOut());
    }

    [HttpPost]
    public async Task<JsonResult> Edit(HtRoleInput input)
    {
        await _roleService.InsertOrUpdate(input);
        return Json("");
    }

    [HttpPost]
    [MaPermission("删除", "Delete")]
    public async Task<JsonResult> Delete(string ids)
    {
        await _roleService.BatchDelete(ids);
        return Json("");
    }

    [HttpPost]
    [MaPermission("设置状态", "SetState")]
    public async Task<JsonResult> SetState(int id, bool state, string type)
    {
        await _roleService.SetState(id, state, type);
        return Json("");
    }

    /// <summary>
    ///     获取权限数据
    /// </summary>
    /// <param name="roleid"></param>
    /// <returns></returns>
    [UnifyResult(typeof(JsonResult))]
    [MaPermission("获取菜单权限数据", "GetPermissionDtree")]
    public async Task<JsonResult> GetPermissionDtree(int roleid = 0)
    {
        var permissionDtree = await _roleService.GetRoleDtreeCheck(roleid);
        return Json(permissionDtree);
    }

    /// <summary>
    ///     设置角色权限
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [MaPermission("设置角色权限", "SetMenuPermission")]
    public async Task<JsonResult> SetMenuPermission(HtRoleInput input)
    {
        await _roleService.SetRolePermission(input);
        return Json("");
    }
}